package cn.bigcontainer.alt.sys.extension;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.bigcontainer.alt.common.util.WoKit;
import cn.bigcontainer.alt.meta.dto.MViewDto;
import cn.bigcontainer.alt.meta.extension.MFormLoader;
import cn.bigcontainer.alt.sys.mapper.SysXiaoxiCsrMapper;
import cn.bigcontainer.alt.sys.mapper.SysXiaoxiJsrMapper;
import cn.bigcontainer.alt.sys.mapper.SysXiaoxiMapper;
import cn.bigcontainer.alt.sys.po.SysXiaoxi;
import cn.bigcontainer.alt.sys.po.SysXiaoxiCsr;
import cn.bigcontainer.alt.sys.po.SysXiaoxiJsr;

/**
 * 加载创建页面扩展，加载回复页面时需要处理发送人、抄送人等字段
 * @author jiaod
 *
 */
@Component
public class SysXiaoxiLoadReplyExtension implements MFormLoader {

	@Resource
	private SysXiaoxiMapper sysXiaoxiMapper;
	
	@Resource
	private SysXiaoxiCsrMapper sysXiaoxiCsrMapper;
	
	@Resource
	private SysXiaoxiJsrMapper sysXiaoxiJsrMapper;
	
	private void setReplyForm(MViewDto createForm, SysXiaoxi sysXiaoxi) {
		// 设置回复消息ID
		createForm.getProperty("cx_sys_xiaoxi_mhfxx0_id").setValue(sysXiaoxi.getMid());
		// 设置原始消息ID
		createForm.getProperty("cx_sys_xiaoxi_mysxx0_id").setValue(sysXiaoxi.getMysxx0Id() == null ? sysXiaoxi.getMid() : sysXiaoxi.getMysxx0Id());
		// 设置标题
		createForm.getProperty("cx_sys_xiaoxi_mbt").setValue("回复：" + sysXiaoxi.getMbt());
		// 将sysXiaoxi的发送人设置为本次回复的接收人
		createForm.getProperty("cx_sys_xiaoxi_r20").setValue("sys_user:" + sysXiaoxi.getMfsr0Id());
	}
	
	@Override
	public MViewDto before(MViewDto fv, String keyValue, Boolean create) {
		return null;
	}

	@Override
	public void after(MViewDto createForm, String keyValue, Boolean create) {
		// 被回复的消息
		SysXiaoxi sysXiaoxi = sysXiaoxiMapper.selectById(createForm.getApp().getObject().getRequest().getString("selectedIds"));
		setReplyForm(createForm, sysXiaoxi);
		// 如果是回复所有，则还需要处理接收人和抄送人
		if (createForm.getApp().getObject().getRequest().bigEquals("replyall", "true")) {
			// 将sysXiaoxi的抄送人设置为本次回复的抄送人
			List<SysXiaoxiCsr> csrs = sysXiaoxiCsrMapper.selectList(new QueryWrapper<SysXiaoxiCsr>().eq("mxiaoxi0_id", sysXiaoxi.getMid()));
			if (csrs.size() > 0) {
				List<String> csrList = new ArrayList<String>();
				for (int i = 0; i < csrs.size(); i++) {
					SysXiaoxiCsr csr = csrs.get(i);
					csrList.add(csr.getRelatedObjectId() + ":" + csr.getInverseProperty0Id());
				}
				createForm.getProperty("cx_sys_xiaoxi_r30").setValue(WoKit.join(csrList));
			}
			// 将sysXiaoxi的发送人和接收人设置为本次回复的接收人
			List<SysXiaoxiJsr> jsrs = sysXiaoxiJsrMapper.selectList(new QueryWrapper<SysXiaoxiJsr>().eq("mxiaoxi0_id", sysXiaoxi.getMid()));
			List<String> jsrList = new ArrayList<String>();
			jsrList.add("sys_user:" + sysXiaoxi.getMfsr0Id());
			for (int i = 0; i < jsrs.size(); i++) {
				SysXiaoxiJsr jsr = jsrs.get(i);
				String jsrValueString = jsr.getRelatedObjectId() + ":" + jsr.getInverseProperty0Id();
				if (jsrList.contains(jsrValueString)) {
					continue;
				}
				jsrList.add(jsrValueString);
			}
			createForm.getProperty("cx_sys_xiaoxi_r20").setValue(WoKit.join(jsrList));
		}
	}

}
